package com.xique.client.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xique.client.bean.domain.ParkOut;
import com.xique.client.bean.search.ParkOutSearch;
import com.xique.client.mapper.ParkOutMapper;
import com.xique.client.service.service.IParkOutService;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2021/11/9 14:42
 */
@Service
public class ParkOutServiceImpl implements IParkOutService {

    @Autowired
    private ParkOutMapper parkOutMapper;

    /**
     * 查询出场记录
     *
     * @param id 出场记录ID
     * @return 出场记录
     */
    @Override
    public ParkOut selectParkOutById(Long id)
    {
        return parkOutMapper.selectById(id);
    }

    @Override
    public ParkOut selectParkOut(ParkOutSearch search) {
        QueryWrapper<ParkOut> wrapper = new QueryWrapper<>();
        if (null != search.getLaneIds()) {
            wrapper.in("out_lane_id", search.getLaneIds());
        }
        if (StringUtils.isNotEmpty(search.getParkingSerial())) {
            wrapper.eq("parking_serial", search.getParkingSerial());
        }
        wrapper.orderByDesc("UNIX_TIMESTAMP(update_time)")
                .last("limit 1");

        return parkOutMapper.selectOne(wrapper);
    }

    /**
     * 查询出场记录列表
     *
     * @param parkOut 出场记录
     * @return 出场记录
     */
    @Override
    public List<ParkOut> selectParkOutList(ParkOut parkOut)
    {
        QueryWrapper<ParkOut> wrapper = new QueryWrapper<>();
        return parkOutMapper.selectList(wrapper);
    }

    /**
     * 新增出场记录
     *
     * @param parkOut 出场记录
     * @return 结果
     */
    @Override
    public int insertParkOut(ParkOut parkOut)
    {
        parkOut.setCreateTime(DateUtils.getNowDate());
        return parkOutMapper.insert(parkOut);
    }

    /**
     * 修改出场记录
     *
     * @param parkOut 出场记录
     * @return 结果
     */
    @Override
    public int updateParkOut(ParkOut parkOut)
    {
        parkOut.setUpdateTime(DateUtils.getNowDate());
        return parkOutMapper.updateById(parkOut);
    }

    /**
     * 删除出场记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteParkOutByIds(List<Long> ids)
    {
        return parkOutMapper.deleteBatchIds(ids);
    }

    /**
     * 删除出场记录信息
     *
     * @param id 出场记录ID
     * @return 结果
     */
    @Override
    public int deleteParkOutById(Long id)
    {
        return parkOutMapper.deleteById(id);
    }
}
